package top.fongs.spring_security_jwt.service.impl.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import top.fongs.spring_security_jwt.service.inter.auth.AuthService;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Base64;

@Service
public class AuthServiceImpl implements AuthService {

    private final AuthenticationManager authenticationManager;

    @Autowired
    public AuthServiceImpl( AuthenticationManager authenticationManager ) {
        this.authenticationManager = authenticationManager;
    }

    @Override
    public Object signIn( String username, String password ) {
        Authentication authenticate = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken( username, password ) );
        return authenticate.getPrincipal( );
    }


    public static void main( String[] args ) {
        String content = "testfdsafsadfdsafasdfadsfsdafsdaf";
        String password = "12345678";
        //加密
        System.out.println( "加密前：" + content );
        byte[] encryptResult = encrypt( content, password );
        String s = Base64.getEncoder( ).encodeToString( encryptResult );
        System.err.println( "加密后： " + s );
        //解密
        byte[] decryptResult = decrypt( encryptResult, password );
        assert decryptResult != null;
        System.out.println( "解密后：" + new String( decryptResult ) );
    }

    /**
     * 加密
     *
     * @param content  需要加密的内容
     * @param password 加密密码
     * @return b
     */
    private static byte[] encrypt( String content, String password ) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance( "AES" );
            kgen.init( 256, new SecureRandom( password.getBytes( ) ) );
            SecretKey secretKey = kgen.generateKey( );
            byte[] enCodeFormat = secretKey.getEncoded( );
            SecretKeySpec key = new SecretKeySpec( enCodeFormat, "AES" );
            Cipher cipher = Cipher.getInstance( "AES" );// 创建密码器
            byte[] byteContent = content.getBytes( "utf-8" );
            cipher.init( Cipher.ENCRYPT_MODE, key );// 初始化
            return cipher.doFinal( byteContent ); // 加密
        } catch ( NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedEncodingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e ) {
            e.printStackTrace( );
        }
        return null;
    }


    /**
     * 解密
     *
     * @param content  待解密内容
     * @param password 解密密钥
     * @return b
     */
    private static byte[] decrypt( byte[] content, String password ) {
        try {
            KeyGenerator kgen = KeyGenerator.getInstance( "AES" );
            kgen.init( 256, new SecureRandom( password.getBytes( ) ) );
            SecretKey secretKey = kgen.generateKey( );
            byte[] enCodeFormat = secretKey.getEncoded( );
            SecretKeySpec key = new SecretKeySpec( enCodeFormat, "AES" );
            Cipher cipher = Cipher.getInstance( "AES" );// 创建密码器
            cipher.init( Cipher.DECRYPT_MODE, key );// 初始化
            return cipher.doFinal( content ); // 加密
        } catch ( NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e ) {
            e.printStackTrace( );
        }
        return null;
    }

}
